package algorithm.leetcode.simple;

/*
 给定两个字符串形式的非负整数 num1 和num2 ，计算它们的和。
 */

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/10/28
 */
public class Question415 {

    public static void main(String[] args) {
        Question415 question = new Question415();
        String sum = question.addStrings("22", "23");
        System.out.println(sum);
    }

    public String addStrings(String num1, String num2) {
        int i = num1.length() - 1;
        int j = num2.length() - 1;
        int sum = 0;
        StringBuilder sb = new StringBuilder();
        while (i >= 0 || j >= 0 || sum != 0) {
            int x = i >= 0 ? num1.charAt(i) - '0' : 0;
            int y = j >= 0 ? num2.charAt(j) - '0' : 0;
            int r = x + y + sum;
            sb.append(r % 10);
            sum = r / 10;
            i--;
            j--;
        }
        return sb.reverse().toString();
    }
}
